package cn.wizzer.app.user.modules.service.impl;

import cn.wizzer.app.user.modules.models.GroupReport;
import cn.wizzer.app.user.modules.models.vo.GroupReportVo;
import cn.wizzer.app.user.modules.services.GroupReportService;
import cn.wizzer.framework.base.service.BaseServiceImpl;
import cn.wizzer.framework.base.service.CommonDBService;
import cn.wizzer.framework.page.Pagination;
import com.alibaba.dubbo.config.annotation.Service;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.lang.Times;

@IocBean(args = {"refer:dao"})
@Service(interfaceClass = GroupReportService.class)
public class GroupReportServiceImpl extends BaseServiceImpl<GroupReport> implements GroupReportService {

    public GroupReportServiceImpl(Dao dao) {
        super(dao);
    }

    @Inject
    private CommonDBService dbService;

    @Override
    public Pagination listPage(int pageNumber, int pageSize, String groupName, String userKeyword, String[] searchDate) {
        Sql sql = Sqls.create("SELECT i.groupName, u.nickName, u.userName, gr.id, gr.ctAt ,gr.content " +
                "FROM group_report gr ");
        Sql joinSql = Sqls.create("LEFT JOIN user u ON u.id = gr.userId " +
                "LEFT JOIN group_info i ON i.id = gr.groupId " +
                "$condition ");
        Cnd cnd = Cnd.where("gr.delFlag", "=", 0);
        if (Strings.isNotBlank(groupName)) {
            groupName = "%" + groupName + "%";
            cnd.and("i.groupName", "LIKE", groupName);
        }
        if (Strings.isNotBlank(userKeyword)) {
            userKeyword = "%" + userKeyword + "%";
            cnd.and(Cnd.exps("u.nickName", "LIKE", userKeyword)
                    .or("u.userName", "LIKE", userKeyword));
        }
        if (searchDate != null && searchDate.length > 0) {
            cnd.and("gr.ctAt", ">=", Times.d2TS(Times.D(searchDate[0])));
            cnd.and("gr.ctAt", "<=", Times.d2TS(Times.D(searchDate[1])));
        }
        joinSql.setCondition(cnd);
        sql.appendSourceSql(joinSql.toString());
        sql.appendSourceSql("ORDER BY gr.ctAt DESC");
        return dbService.listPage(GroupReportVo.class, pageNumber, pageSize, sql.toString());
    }


}
